package com.example.demo.controller;



import com.example.demo.entity.Sch_remind;
import com.example.demo.entity.User;
import com.example.demo.pojo.PlannerResult;
import com.example.demo.repository.Sch_remindRepository;
import com.example.demo.repository.UserRepository;
import com.example.demo.time.Executetime;
import com.example.demo.utils.ThreadLocalUtil;
import com.example.demo.vo.Sch_remindVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/schuleremind")
public class SchRemindController {

    @Autowired
    Sch_remindRepository schRemindRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    Executetime executetime;

    //建日程提醒
    @PostMapping("/create")
    public PlannerResult<String> create(@RequestBody Sch_remindVo schRemindVo)
    {

        Map<String,Object> map= ThreadLocalUtil.get();
        Integer userid=(Integer) map.get("id");

        Sch_remind schRemind = new Sch_remind();
        schRemind.setMethod(schRemindVo.getMethod());
        schRemind.setTime(schRemindVo.getTime());
        schRemind.setUserid(userid);

        Sch_remind result = schRemindRepository.save(schRemind);
        User user = userRepository.findById(schRemind.getUserid()).get();


        if(result.getId()!=null)
        {
            //在此建立发送提醒
//            Executetime executetime = new Executetime();
            executetime.timing_sch(schRemind.getTime(),schRemind,user);
            return PlannerResult.success("建立日程提醒成功");

        }else {
            return PlannerResult.error("创建失败");
        }
    }


}
